package web.servlet;

import domain.PageBean;
import domain.User;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 分页功能的实现（包含数据的展示）
 * 查询数据的展示
 * @author Mc.Chai
 * @program: JavaWeb
 * @create 2022-04-04-12:23 12:23
 */
@WebServlet("/findUserByPageServlet")
public class FindUserByPageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        //获取请求信息中当前页面页码
        String currentPage = request.getParameter("currentPage");
        //获取请求信息中的每页显示的条数
        String rows = request.getParameter("rows");
        //防止空指针异常
        if (currentPage==null||"".equals(currentPage)){
            currentPage="1";
        }
        if (rows==null||"".equals(rows)){
            rows="5";
        }
        //获取查询功能中的请求信息(name,homeTown,email)
        Map<String, String[]> condition = request.getParameterMap();
        //进行查询数据框的回显
        /*Set<String> keys = condition.keySet();
        for (String key : keys) {
            String value = condition.get(key)[0];
            request.setAttribute(key,value);
        }*/

        request.setAttribute("condition",condition);
        UserService service=new UserServiceImpl();
        PageBean<User> pageBean= service.findUserByPage(currentPage,rows,condition);
        request.setAttribute("pageBean",pageBean);
        request.getRequestDispatcher("/list.jsp").forward(request,response);

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
